<?php
/**
 * 持仓查询
 */
namespace app\data\controller;
use \think\Controller;
use \think\Db;

class Hold extends Controller
{
    public function index()
    {

        //查询系统内置用户表用于比对
        $sys_user=db('add_name')->field('account,id')->select();

        //url
        $url_user=LOGIN_HOST.'/trader/proxy/organize/query/orders/position/page';
        $url_user_data = array (
            'search_LIKE_sysRelationUserOrg.userSecurity.loginName' => '',
            'search_LLIKE_sysRelationUserOrg.organizeRelation.orgCode' => '',
            'search_EQ_productId'=>'',
            'search_EQ_direct'=>'',
            'page'=>'1',
            'limit'=>'10000'
        );

        $content = GetPost($url_user, COOKIE_PATH,$url_user_data);

        //过滤采集数据
        $data=json_decode($content);
        //输出内容
        $res=$data->results->content;


        //清空客户表准备重新写入信息
        if(empty($content)){
            sendMail(['6325610@qq.com'=>'兴蜀采集','381508990@qq.com'=>'兴蜀采集'],'采集失败-持仓查询',$content);
            return ;
        }else{
            $delete_sql="truncate table ke_hold";
            Db::execute($delete_sql);
        }

        //批量写入数据库
        foreach ($res as $it){
            $arr=array();
                //交易账号
                $arr['account']=$it->clientNo;
                //用户名
                $arr['user_name']=$it->userName;
                //商品名称
                $arr['goods']=$it->productName;
                //结余
                $arr['type']=$it->direct;
                //持仓量
                $arr['num']=$it->tradeVol;
                //持仓价
                $arr['price']=$it->tradePriceRmb;
                //占用保证金
                $arr['bail_value']=$it->keepDepositRmb;

            //解析所属人
                foreach ($sys_user as $sys){
                    if($arr['account'] == $sys['account'] ){
                        $arr['pid'] = $sys['id'];
                        break;
                    }
                }

                db("hold")->insert($arr);

        }

        //开始发送邮件
        if(date('i')=='00'){
            sendMail(['381508990@qq.com'=>'兴蜀采集'],'采集成功-持仓信息查询',$content);
        }

    }

}
